﻿using Entity.Models;
using Entity.Models.Base;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity
{
    public class DBContext : DbContext
    {
        protected DBContext()
        {
        }
        public DBContext(DbContextOptions options) : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
        }

        public DbSet<CompanyInfo> companyInfos { get; set; }
        public DbSet<DealFlagType>  dealFlagTypes { get; set; }
        public DbSet<DeviceType> deviceTypes { get; set; }
        public DbSet<EventClass> eventClasses { get; set; }
        public DbSet<EventLevel> eventLevels { get; set; }
        public DbSet<EventType> eventTypes { get; set; }
        public DbSet<FactoryType> factoryTypes { get; set; }
        public DbSet<MsgChannelType> msgChannelTypes { get; set; }
        public DbSet<ProtocolType> protocolTypes { get; set; }
        public DbSet<RightInfo> rightInfos { get; set; }

        public DbSet<AlarmGroup> alarmGroups { get; set; }
        public DbSet<AlarmGroupDeviceBind> deviceBinds { get; set; }
        public DbSet<AlarmGroupOfficerBind> officerBinds { get; set; }
        public DbSet<AlarmModel> alarmModels { get; set; }
        public DbSet<AlarmRule> alarmRules { get; set; }
        public DbSet<DeviceInfo> deviceInfos { get; set; }
        public DbSet<DutyOfficer> dutyOfficers { get; set; }
        public DbSet<MsgRegInfo> msgRegInfos { get; set; }
        public DbSet<OperatorInfo> operatorInfos { get; set; }
        public DbSet<RoleInfo> roleInfos { get; set; }
        public DbSet<Settings> Settings { get; set; }


        public DbSet<AlarmRecord> alarmRecords { get; set; }
        public DbSet<MessageNotice> messageNotices { get; set; }
        public DbSet<MsgPushRecord> msgPushRecords { get; set; }
        public DbSet<OperateRecord> operateRecords { get; set; }
    }

    public class BaseContext
    {
        static string defaultConn = "Server=114.55.174.195;port=3306;Database=HuaRunMiddleDB;Uid=admin;Pwd=GZyb2021@#1;Character Set=utf8;";
        public static DBContext CreateContext(string connString = "")
        {
            if (connString == null)
            {
                connString = defaultConn;
            }
            var optionBuilder = new DbContextOptionsBuilder<DBContext>();

            optionBuilder.UseMySql(connString, ServerVersion.AutoDetect(connString)).UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
            optionBuilder.EnableSensitiveDataLogging();
            var context = new DBContext(optionBuilder.Options);
            return context;
        }
    }
}
